# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2020/11/20 19:29
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : natural_spline_interpolation.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
import numpy as np
from scipy.interpolate import CubicSpline,CubicHermiteSpline
import matplotlib.pyplot as plt
import pylab as pl


def draw(x_i, x_range):
    # 绘制要求插出点的散点图
    plt.scatter(x_i, ploy(x_i), label='插出点', color='green')
    # 绘制指定三次样条插值函数横坐标范围内的图形
    plt.plot(x_range, ploy(x_range), label='$S(x)$', color='red')
    plt.title("三次样条插值S(x)")
    pl.rcParams['font.sans-serif'] = ['SimHei']
    pl.rcParams['axes.unicode_minus'] = False
    plt.legend(loc="upper right")
    plt.show()


if __name__ == '__main__':
    # 5个已知插值结点
    x = np.arange(0.2, 1.2, 0.2)
    y = np.array([0.98, 0.92, 0.81, 0.64, 0.38])
    # 4个插出点
    i_set = np.array([0, 1, 10, 11])
    x_set = 0.08 * i_set + 0.2
    # 三次样条插值函数横坐标范围
    range_x = np.linspace(-1, 1.2, 100)
    ploy = CubicSpline(x, y, bc_type='natural')
    draw(x_set, range_x)
